<?php
Yii::import('application.components.login.class.LoginAuth');
Yii::import('application.components.login.class.LoginIdentity');
class FormAuth extends LoginAuth{
	
		
	public $email;
	public $password;
	public $user_name;
	
	public function rules()
	{
		return array(
			array('user_name, password','required'),
			//array('user_name','unique','className'=>'Customer'),
			array('password','loginExist'),
		);
	}
	
	public function loginExist($attribute,$params){
		$customer = new Customer();
		$customer = $customer->findByAttributes(array(
		    'user_name' => $this->user_name,		    
		));
		if (!empty($customer)){
		    $check_pass=$this->ppb_password_hash($this->password, $customer->salt);
		    if($customer->password!=$check_pass){
		        $this->addError($attribute,'Password or username is uncorrect!');
		    }
			if($customer->status==1){
				$this->setPrimary($customer->id);
				$this->setUsername($customer->user_name);
			}else{
				$this->addError($attribute,'Your account has been frozen, contact the administrator!');
			}
			
		}else{
			$this->addError($attribute,'Password or username is uncorrect!');
		}
	}
	function ppb_password_hash ($password, $salt)
	{
	    return md5(md5($password) . $salt);
	}
	//20150715 sxw
	public function authenticate(){
		$valid = $this->valid();
		
		return $valid;
	}
	
}